package com.cloudeasy.label.listener;

import com.cloudeasy.api.constant.AmqpConstant;
import com.cloudeasy.api.util.JsonUtil;
import com.cloudeasy.api.vo.amqp.AreaItemIncVO;
import com.cloudeasy.api.vo.amqp.LabelItemIncVO;
import com.cloudeasy.label.service.LabelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Component
@Slf4j
public class ItemListener {
    private final LabelService labelService;

    public ItemListener(LabelService labelService) {
        this.labelService = labelService;
    }


    @RabbitListener(queuesToDeclare = @Queue(AmqpConstant.LabelConstant.LABEL_ITEM_ROUTING_KEY))
    public void process(String message) {
        LabelItemIncVO labelItemIncVO = JsonUtil.fromJson(message, LabelItemIncVO.class);
        log.info("标签 {} 项变更 {}", Objects.requireNonNull(labelItemIncVO).getLabelIds(), labelItemIncVO.getInc());
        labelService.itemIncByLabelIds(labelItemIncVO.getLabelIds(), labelItemIncVO.getInc());
    }
}
